package com.jackeys.piaems.util;


import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @ClassName: EmailUtil
 * @Description: 发送邮件的工具类
 * @Author: Jackeys
 * @Date: 2021-06-22 15:36
 * @Version: 1.0
 */
public class EmailUtil {

    public static boolean sendEmail(String email, String content) throws MessagingException {
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");
        properties.put("mail.smtp.host", "smtp.qq.com");
        properties.put("mail.smtp.port", 465);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");
        Session session = Session.getInstance(properties);
        session.setDebug(true);
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("1623427947@qq.com"));
        InternetAddress to = new InternetAddress(email);
        message.setRecipient(MimeMessage.RecipientType.TO, to);
        message.setSubject("PIAEMS");
        message.setText("欢迎您注册piaems记账系统，您本次的验证码为：" + content);
        Transport transport = session.getTransport();
        transport.connect("1623427947@qq.com", "cyxymaxprybmhhhc");
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        return true;
    }

    public static String getSecurityCode(int cnt) {
        int multiple = 1;
        for (int i = 0; i < cnt - 1; i++) {
            multiple *= 10;
        }
        int newNum = (int) ((Math.random() * 9 + 1) * multiple);
        return String.valueOf(newNum);
    }
}
